﻿using System;
using System.Windows.Forms;
using OrionApplication.OrionLib;
using OrionApplication.OrionLib.Dao;
using OrionApplication.OrionLib.Orion;
using System.Collections.Generic;

namespace OrionApplication.OrionForm
{
    public partial class OrionMenuForm : OrionBaseDoubleForm
    {
        private Int32 OrionMenuIdA;

        public OrionMenuForm()
            : base()
        {
            dao = new DaoOrionMenu();
            InitializeComponent();
        }

        public OrionMenuForm(String titre)
            : base(titre)
        {
            dao = new DaoOrionMenu();
            InitializeComponent();
        }

        private void btnSpecActuMenu_Click(object sender, EventArgs e)
        {
            this.RempliCb();
        }

        private void RempliCb()
        {
            kCbMenuIdParent.DataSource = ((DaoOrionMenu)dao).SelectAll();
        }

        private void RempliskCbMenu()
        {
            DaoOrionTable dao = new DaoOrionTable();
            List<OrionTable> lMenu = dao.SelectByIdent("MENU", true);
            kCbMenuOrionTable.DataSource = lMenu;
        }

        private void DupliqueEnregistrement()
        {
            if (kDgv.SelectedRows.Count > 0)
            {
                if (OrionOutils.ChoixOuiNon(OrionConfBase.ConfirmDuplique) == DialogResult.Yes)
                {
                    OrionMenu om = ((DaoOrionMenu)dao).SelectById(Convert.ToInt32(kDgv.SelectedRows[0].Cells["OrionId"].Value));
                    om = ((DaoOrionMenu)dao).Insert(om);
                    this.RempliKDgv();
                    this.FocusSur(om.OrionId);
                }
            }
            else
                OrionOutils.Alerte(OrionConfBase.AucuneLigneSelectionnee);
        }

        private void RempliForm(OrionMenu om)
        {
            if (om.OrionMenuIdParent == 0)
            {
                kCbHasParent.Checked = false;
                kCbMenuIdParent.Enabled = false;
            }
            else
            {
                kCbHasParent.Checked = true;
                kCbMenuIdParent.Enabled = true;
                kCbMenuIdParent.SelectedValue = om.OrionMenuIdParent;
            }
            kTbLibelle.Text = om.Libelle;
            kNudOrdre.Value = om.Ordre;
            kTbOrionParam.Text = om.OrionParam;
            kTbClasseName.Text = om.ClasseName;
            kTbFormName.Text = om.FormName;
            kTbFormTitle.Text = om.FormTitle;
            kCbMenuOrionTable.SelectedValue = om.OrionMenuId;
            foreach (ListViewItem lvi in listView1.SelectedItems)
                lvi.Selected = false;
            listView1.Focus();
            listView1.Items[om.OrionImageId].Selected = true;
            listView1.Items[om.OrionImageId].EnsureVisible();
        }

        private void SauvegardeForm()
        {
            OrionMenu om = this.RempliObject();
            if (kLblMode.Text == OrionConfBase.OrionModeInsert)
                om = ((DaoOrionMenu)dao).Insert(om);
            else
            {
                if (kDgv.SelectedRows.Count > 0)
                {
                    om.OrionId = Convert.ToInt32(kDgv.SelectedRows[0].Cells["OrionId"].Value);
                    om = ((DaoOrionMenu)dao).Update(om);

                }
                else
                    OrionOutils.Alerte(OrionConfBase.AucuneLigneSelectionnee);
            }
            this.RempliKDgv();
            this.FocusSur(om.OrionId);
        }

        private OrionMenu RempliObject()
        {
            OrionMenu retour = new OrionMenu();
            if (kCbHasParent.Checked)
                retour.OrionMenuIdParent = (Int32)kCbMenuIdParent.SelectedValue;
            else
                retour.OrionMenuIdParent = 0;
            retour.Libelle = kTbLibelle.Text;
            retour.Ordre = Convert.ToInt32(kNudOrdre.Value);
            retour.OrionImageId = listView1.SelectedItems[0].ImageIndex;
            retour.OrionParam = kTbOrionParam.Text;
            retour.ClasseName = kTbClasseName.Text;
            retour.FormName = kTbFormName.Text;
            retour.FormTitle = kTbFormTitle.Text;
            retour.OrionMenuId = Convert.ToInt32(kCbMenuOrionTable.SelectedValue);
            return retour;
        }

        public void Show(Int32 OrionMenuIdP)
        {
            this.OrionMenuIdA = OrionMenuIdP;
            this.RempliCb();
            ImageList il = ((Main)this.MdiParent).OrionImageListe;
            listView1.SmallImageList = il;
            for (int i = 0; i < il.Images.Count; i++)
            {
                listView1.Items.Add(new ListViewItem("Image n°" + i, i));
            }
            base.Show();

        }

        private void GestionDroitUserLigne()
        {
            OrionMenuDroit omd = new OrionMenuDroit("Gestion des Droits Utilisateurs sur " + this.kTbLibelle.Text, "USER", Convert.ToInt32(kDgv.SelectedRows[0].Cells["OrionId"].Value));
            omd.NameFormA = "OrionMenuDroit";
            omd.ShowDialog();
        }

        private void GestionDroitGroupLigne()
        {
            OrionMenuDroit omd = new OrionMenuDroit("Gestion des Droits Groupes sur " + this.kTbLibelle.Text, "GROUP", Convert.ToInt32(kDgv.SelectedRows[0].Cells["OrionId"].Value));
            omd.NameFormA = "OrionMenuDroit";
            omd.ShowDialog();
        }

        public void RowEnter()
        {
            if (kDgv.SelectedRows.Count > 0)
            {
                OrionMenu om = ((DaoOrionMenu)dao).SelectById(Convert.ToInt32(kDgv.SelectedRows[0].Cells["OrionId"].Value));
                this.RempliForm(om);
            }
        }

        private void kCbHasParent_CheckedChanged(object sender, EventArgs e)
        {
            kCbMenuIdParent.Enabled = kCbHasParent.Checked;
        }

        private void buttonSpecAny1_Click(object sender, EventArgs e)
        {
            this.RempliskCbMenu();
        }
    }
}
